package com.yanyeori.rabbitmqconsumer.config;

import com.yanyeori.rabbitmqconsumer.constant.RabbitmqConfig;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author chenkuan
 * @version v1.0
 * @desc Fanout Exchange(扇形交换机)：发送到交换机的数据会放到每个通道，每个通道都可消费到同样的数据
 * @date 20/7/30 030 16:38
 */
@Configuration
public class FanoutRabbitConfig {
	/**
	 * 创建三个队列 ：fanout.A   fanout.B  fanout.C
	 * 将三个队列都绑定在交换机 fanoutExchange 上
	 * 因为是扇型交换机, 路由键无需配置, 配置也不起作用
	 */

	@Bean
	public Queue fanoutQueueA() {
		return new Queue(RabbitmqConfig.FANOUT_QUEUE_A);
	}

	@Bean
	public Queue fanoutQueueB() {
		return new Queue(RabbitmqConfig.FANOUT_QUEUE_B);
	}

	@Bean
	public Queue fanoutQueueC() {
		return new Queue(RabbitmqConfig.FANOUT_QUEUE_C);
	}

	@Bean
	public FanoutExchange fanoutExchange() {
		return new FanoutExchange(RabbitmqConfig.FANOUT_EXCHANGE);
	}

	@Bean
	public Binding fanoutBindingA(@Qualifier("fanoutQueueA") Queue queue, @Qualifier("fanoutExchange") FanoutExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange);
	}

	@Bean
	public Binding fanoutBindingB(@Qualifier("fanoutQueueB") Queue queue, @Qualifier("fanoutExchange") FanoutExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange);
	}

	@Bean
	public Binding fanoutBindingC(@Qualifier("fanoutQueueC") Queue queue, @Qualifier("fanoutExchange") FanoutExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange);
	}
}
